package com.hzw.saas.common.util.exception;

import java.util.Objects;

import lombok.Getter;

/**
 * 冲突异常，一般为
 *
 * @author zzl
 * @since 09/09/2021
 */
@Getter
public abstract class BaseRuntimeException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 是否在日志中打印错误堆栈，优先级高于printMsg
     */
    protected boolean printTrack = false;

    /**
     * 是否在日志中打印错误信息
     */
    protected boolean printMsg = true;

    /**
     * 业务状态码
     */
    protected String bizCode;

    /**
     * 业务信息
     */
    protected String bizMsg;

    /**
     * 获取异常的HTTP状态码
     *
     * @return http状态码
     */
    public abstract Integer httpStatus();

    /**
     * 设置 printTrack
     *
     * @param printTrack true|false
     * @return BaseException子类
     */
    public abstract <T extends BaseRuntimeException> T setPrintTrack(boolean printTrack);

    /**
     * 设置 printTrack
     *
     * @param printMsg true|false
     * @return BaseException子类
     */
    public abstract <T extends BaseRuntimeException> T setPrintMsg(boolean printMsg);

    /**
     * 设置业务状态码
     *
     * @param bizCode 业务状态码
     * @return BaseException子类
     */
    public abstract <T extends BaseRuntimeException> T setBizCode(String bizCode);

    /**
     * 设置业务信息
     *
     * @param bizMsg 业务信息
     * @return BaseException子类
     */
    public abstract <T extends BaseRuntimeException> T setBizMsg(String bizMsg);

    /**
     * 初始化方法
     */
    protected abstract void postConstruct();

    protected BaseRuntimeException(String msg) {
        super(msg);
        this.bizMsg = Objects.isNull(msg) ? "" : msg;
        this.postConstruct();
    }

    protected BaseRuntimeException(String msg, Throwable cause) {
        super(msg, cause);
        this.bizMsg = Objects.isNull(msg) ? "" : msg;
        this.postConstruct();
    }
}
